home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu017.dms / pu017.adf / Gadgets / Example6.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  10KB  |  325 lines

  1. /* Example6                                                           */
  2. /* This program will open a normal window which is connected to the   */
  3. /* Workbench Screen. The window will use all System Gadgets, and will */
  4. /* close first when the user has selected the System gadget Close     */
  5. /* window. Inside the window we have put a Boolean gadget with a      */
  6. /* connecting mask. The gadget will only be highlighted when the user */
  7. /* selects this gadget while pointing inside the specified (masked)   */
  8. /* area.                                                              */
  9.  
  10.  
  11.  
  12. /* If your program is using Intuition you should include intuition.h: */
  13. #include <intuition/intuition.h>
  14.  
  15.  
  16.  
  17. struct IntuitionBase *IntuitionBase;
  18.  
  19.  
  20.  
  21. /* Image data for the gadget: */
  22. USHORT chip my_image_data[32]=
  23. {
  24.    0xFFFF,0xFFFF, /* Bitplane ZERO */
  25.    0xFFF8,0x1FFF,
  26.    0xFFE0,0x07FF,
  27.    0xFF80,0x01FF,
  28.    0xFE00,0x007F,
  29.    0xF800,0x001F,
  30.    0xE000,0x0007,
  31.    0x8000,0x0001,
  32.    0x8000,0x0001,
  33.    0xE000,0x0007,
  34.    0xF800,0x001F,
  35.    0xFE00,0x007F,
  36.    0xFF80,0x01FF,
  37.    0xFFE0,0x07FF,
  38.    0xFFF8,0x1FFF,
  39.    0xFFFF,0xFFFF
  40. };
  41.  
  42. /* Image structure for the gadget: */
  43. struct Image my_image=
  44. {
  45.   0, 0,          /* LeftEdge, TopEdge */
  46.   32, 16,        /* Width, Height */
  47.   1,             /* Depth */
  48.   my_image_data, /* ImageData */
  49.   0x01, 0x00,    /* PlanePick, PlaneOnOff */
  50.   NULL           /* NextImage */
  51. };
  52.  
  53.  
  54.      UWORD chip my_mask[32]=
  55. {
  56.    0x0000,0x0000, /* Bitplane ZERO */
  57.    0x0007,0xE000,
  58.    0x001F,0xF800,
  59.    0x007F,0xFE00,
  60.    0x01FF,0xFF80,
  61.    0x07FF,0xFFE0,
  62.    0x1FFF,0xFFF8,
  63.    0x7FFF,0xFFFE,
  64.    0x7FFF,0xFFFE,
  65.    0x1FFF,0xFFF8,
  66.    0x07FF,0xFFE0,
  67.    0x01FF,0xFF80,
  68.    0x007F,0xFE00,
  69.    0x001F,0xF800,
  70.    0x0007,0xE000,
  71.    0x0000,0x0000
  72. };
  73.  
  74. /* The BoolInfo structure fot the gadget: */
  75. struct BoolInfo my_bool_info=
  76. {
  77.     BOOLMASK,  /* Flags, for the moment this is the only flag you may use. */
  78.     my_mask,   /* Mask, pointer to our bit mask. Only when the user clicks */
  79.                /* inside the small area of the gadget it will be selected, */
  80.                          /* and only that area will be highlighted. */
  81.                          /* Remember! The width and height of the mask data must */
  82.                          /* be the same as the width and height of the gadget. */
  83.   0          /* Reserved, set this variable to 0 for the moment. */
  84. };
  85.  
  86. /* The Gadget structure: */
  87. struct Gadget my_gadget=
  88. {
  89.   NULL,          /* NextGadget, no more gadgets in the list. */
  90.   40,            /* LeftEdge, 40 pixels out. */
  91.   20,            /* TopEdge, 20 lines down. */
  92.   32,            /* Width, 32 pixels wide. */
  93.   16,            /* Height, 16 pixels lines heigh. */
  94.   GADGHCOMP|     /* Flags, complement the colours when selected. */
  95.   GADGIMAGE,     /* Render the gadget with an Image structure. */
  96.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  97.   RELVERIFY|     /* the user has selected this gadget, and when the user */
  98.                  /* has released it. */ 
  99.   BOOLEXTEND,    /* This gadget has an BoolInfo connected to it. */ 
  100.   BOOLGADGET,    /* GadgetType, a Boolean gadget. */
  101.   (APTR) &my_image, /* GadgetRender, a pointer to our Image structure. */
  102.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  103.                  /* with an alternative image. (We complement the */
  104.                  /* colours instead) */
  105.   NULL,          /* GadgetText, no text connected to the gadget. */
  106.                  /* (See chapter 3 GRAPHICS for more information) */
  107.   NULL,          /* MutualExclude, no mutual exclude. */
  108.   (APTR) &my_bool_info, /* SpecialInfo, pointer to the BoolInfo str. */
  109.   0,             /* GadgetID, no id. */
  110.   NULL           /* UserData, no user data connected to the gadget. */
  111. };
  112.  
  113.  
  114.  
  115. /* Declare a pointer to a Window structure: */ 
  116. struct Window *my_window;
  117.  
  118. /* Declare and initialize your NewWindow structure: */
  119. struct NewWindow my_new_window=
  120. {
  121.   50,            /* LeftEdge    x position of the window. */
  122.   25,            /* TopEdge     y positio of the window. */
  123.   200,           /* Width       200 pixels wide. */
  124.   100,           /* Height      100 lines high. */
  125.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  126.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  127.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  128.                  /*             user has selected the Close window gad, */
  129.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  130.   GADGETUP,      /*             a gadge has been released. */
  131.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  132.   WINDOWCLOSE|   /*             Close Gadget. */
  133.   WINDOWDRAG|    /*             Drag gadget. */
  134.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  135.   WINDOWSIZING|  /*             Sizing Gadget. */
  136.   ACTIVATE,      /*             The window should be Active when opened. */
  137.   &my_gadget,    /* FirstGadget A pointer to my_gadget structure. */
  138.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  139.   "TOUCH ME",    /* Title       Title of the window. */
  140.   NULL,          /* Screen      Connected to the Workbench Screen. */
  141.   NULL,          /* BitMap      No Custom BitMap. */
  142.   140,           /* MinWidth    We will not allow the window to become */
  143.   50,            /* MinHeight   smaller than 140 x 50, and not bigger */
  144.   300,           /* MaxWidth    than 300 x 200. */
  145.   200,           /* MaxHeight */
  146.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  147. };
  148.  
  149.  
  150.  
  151. main()
  152. {
  153.   /* Boolean variable used for the while loop: */
  154.   BOOL close_me;
  155.  
  156.   /* Declare a variable in which we will store the IDCMP flag: */
  157.   ULONG class;
  158.   
  159.   /* Declare a pointer to an IntuiMessage structure: */
  160.   struct IntuiMessage *my_message;
  161.  
  162.  
  163.  
  164.   /* Before we can use Intuition we need to open the Intuition Library: */
  165.   IntuitionBase = (struct IntuitionBase *)
  166.     OpenLibrary( "intuition.library", 0 );
  167.   
  168.   if( IntuitionBase == NULL )
  169.     exit(); /* Could NOT open the Intuition Library! */
  170.  
  171.  
  172.  
  173.   /* We will now try to open the window: */
  174.   my_window = (struct Window *) OpenWindow( &my_new_window );
  175.   
  176.   /* Have we opened the window succesfully? */
  177.   if(my_window == NULL)
  178.   {
  179.     /* Could NOT open the Window! */
  180.     
  181.     /* Close the Intuition Library since we have opened it: */
  182.     CloseLibrary( IntuitionBase );
  183.  
  184.     exit();  
  185.   }
  186.  
  187.  
  188.  
  189.   /* We have opened the window, and everything seems to be OK. */
  190.  
  191.  
  192.  
  193.   close_me = FALSE;
  194.  
  195.   /* Stay in the while loop until the user has selected the Close window */
  196.   /* gadget: */
  197.   while( close_me == FALSE )
  198.   {
  199.     /* Wait until we have recieved a message: */
  200.     Wait( 1 << my_window->UserPort->mp_SigBit );
  201.  
  202.     /* Collect the message: */
  203.     my_message = (struct IntuiMessage *) GetMsg( my_window->UserPort );
  204.  
  205.     /* Have we collected the message sucessfully? */
  206.     if(my_message)
  207.     {
  208.       /* After we have collected the message we can read it, and save any */
  209.       /* important values which we maybe want to check later: */
  210.       class = my_message->Class;
  211.  
  212.       /* After we have read it we reply as fast as possible: */
  213.       /* REMEMBER! Do never try to read a message after you have replied! */
  214.       /* Some other process has maybe changed it. */
  215.       ReplyMsg( my_message );
  216.  
  217.       /* Check which IDCMP flag was sent: */
  218.       switch( class )
  219.       {
  220.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  221.                close_me=TRUE;
  222.                break;
  223.              
  224.         case GADGETDOWN:   /* The user has pressed on the Boolean gadget. */
  225.                printf("Down\n");
  226.                break;
  227.              
  228.         case GADGETUP:     /* The user has released the Boolean gadget. */
  229.                printf("Up\n");
  230.                break;
  231.       }
  232.     }
  233.   }
  234.  
  235.  
  236.  
  237.   /* We should always close the windows we have opened before we leave: */
  238.   CloseWindow( my_window );
  239.  
  240.  
  241.  
  242.   /* Close the Intuition Library since we have opened it: */
  243.   CloseLibrary( IntuitionBase );
  244.   
  245.   /* THE END */
  246. }
  247.  
  248.  
  249.  
  250. /*******************************************/
  251. /* Extra Information about masked gadgets: */
  252. /*******************************************/
  253.  
  254. /*
  255.  
  256. What is special about this example is that we have connected a mask to the
  257. Boolean gadget. The gadget will therefore only be highlighted when the user
  258. clicks inside the "masked" area, and only that area will be highlighted.
  259.  
  260. Only Boolean gadgets may have a connecting mask, and if you would want to
  261. use one you need to:
  262.  
  263.   1. Set the BOOLEXTEND flag in the Activation field.
  264.  
  265.   2. Declare and initialize the bit mask data. Important, the mask must be
  266.      exactly as high and wide as the gadget itself. Only the selected parts
  267.      (masked = 1's) will be sensetive, and highlighted when selected.
  268.  
  269.   3. Declare and initialize a BoolInfo structure which look like this:
  270.      struct BoolInfo
  271.      {
  272.        USHORT Flags;
  273.        UWORD *Mask;
  274.        ULONG Reserved;
  275.      };
  276.      
  277.      Flags:    There exist for the moment only one flag, BOOLMASK. Set it.
  278.      Mask:     Pointer to the bit mask data.
  279.      Reserved: Reserved field. Set it to 0.
  280.  
  281.   4. Set the SpecialInfo pointer in the Gadget structure to point at a
  282.      BoolInfo structure.
  283.  
  284.  
  285.  
  286. In this example the gadget look like this:              
  287.                                              
  288. 1111111111111111 1111111111111111    0: blue 
  289. 1111111111111000 0001111111111111    1: white
  290. 1111111111100000 0000011111111111
  291. 1111111110000000 0000000111111111
  292. 1111111000000000 0000000001111111
  293. 1111100000000000 0000000000011111
  294. 1110000000000000 0000000000000111
  295. 1000000000000000 0000000000000001
  296. 1000000000000000 0000000000000001
  297. 1110000000000000 0000000000000111
  298. 1111100000000000 0000000000011111
  299. 1111111000000000 0000000001111111
  300. 1111111110000000 0000000111111111
  301. 1111111111100000 0000011111111111
  302. 1111111111111000 0001111111111111
  303. 1111111111111111 1111111111111111
  304.  
  305. And the mask look like this:
  306.  
  307. 0000000000000000 0000000000000000    0: Unselected (unmasked) area.
  308. 0000000000000111 1110000000000000    1: Selected (masked) area.
  309. 0000000000011111 1111100000000000
  310. 0000000001111111 1111111000000000
  311. 0000000111111111 1111111110000000
  312. 0000011111111111 1111111111100000
  313. 0001111111111111 1111111111111000
  314. 0111111111111111 1111111111111110
  315. 0111111111111111 1111111111111110
  316. 0001111111111111 1111111111111000
  317. 0000011111111111 1111111111100000
  318. 0000000111111111 1111111110000000
  319. 0000000001111111 1111111000000000
  320. 0000000000011111 1111100000000000
  321. 0000000000000111 1110000000000000
  322. 0000000000000000 0000000000000000
  323.  
  324. */
  325.